package com.leetcode.string.demo1;

/**
 * @Author Zan
 * @Create 2024/5/16 8:58
 * @ClassName: Test
 * @Description : https://leetcode.cn/problems/reverse-string/description/  反转字符串
 */
public class Test {
    public static void main(String[] args) {
        char[] s = new char[]{'h','e','l','l','o'};
        Solution solution = new Solution();;
        solution.reverseString(s);
    }
}

class Solution {
    public void reverseString(char[] s) {
        char[] result = new char[s.length];
        int j = 0;
        for (int i = s.length - 1; i >= 0; i--) {
            result[j++] = s[i];
        }
        System.out.println(result);
    }
}

// 只能修改该数组，不能用其他数组
class Solution2 {
    public void reverseString(char[] s) {
        int start = 0;
        int end = s.length - 1;
        while (start < end) {
            char temp = s[start];
            s[start] = s[end];
            s[end] = temp;
            start++;
            end--;
        }
    }
}

